package com.voicenews.aggregator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.voicenews.dao.ArticleDAO;
import com.voicenews.model.Article;
import com.voicenews.model.DAO;
import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;

public class SaveNewsToDatabaseHandler {

	public static List<Article> saveToDatabase(DAO dao, List<Article> articles,
			long feedId) {
		AdditionTagToArticle additionTagToArticle = new AdditionTagToArticle();

		for (int j = 0; j < articles.size(); j++) {
			Article article = articles.get(j);
			article.setFeedId(feedId);
			String content = null;
			try {
				content = ArticleExtractor.INSTANCE.getText(article.getUrl());
			} catch (BoilerpipeProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				content = "";
			}

			if (article.getDescription() == null) {
				article.setDescription("");
			}

			if (article.getImgLink() == null) {
				article.setImgLink("");
			}

			article.setContent(content);
			// dao.insertArticle(article, content);
			ArticleDAO articleDAO = new ArticleDAO();
			long id = articleDAO.add(article);
			article.setArticleId(id);
			URL urlTTS;
			try {
				byte buf[] = new byte[1024];
				int len;
				File file = new File(System.getProperty("catalina.base")
						+ "\\webapps\\audio\\"
						// + "\\"
						+ id + ".mp3");
				System.out.println(file.getAbsolutePath());
				content = content.replace("\n", " ");
				content = content.replace("\t", " ");
				content = content.replace(" ", "%20");
				urlTTS = new URL(
						"http://localhost/TTSService/Service/service.svc/TTS?voiceIndex=1&rate=2&extension=mp3&text="
								+ content.replace(" ", "%20"));
				System.out.println(urlTTS.toString());
				HttpURLConnection urlConn = (HttpURLConnection) urlTTS
						.openConnection();
				urlConn.addRequestProperty("User-Agent",
						"Mozilla/9.0 (compatible; MSIE 6.0; Windows NT 5.0)");
				InputStream sound = urlConn.getInputStream();
				FileOutputStream out = new FileOutputStream(file, true);
				while ((len = sound.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.close();
				sound.close();

				// System.out.println(file.getAbsolutePath());
				// List<InputStream> list = new ArrayList<InputStream>();
				// Enumeration<InputStream> streamList;
				//
				// Reader reader = new StringReader(content);
				// DocumentPreprocessor dp = new DocumentPreprocessor(reader);
				// List<String> sentenceList = new LinkedList<String>();
				// Iterator<List<HasWord>> it = dp.iterator();
				// while (it.hasNext()) {
				// StringBuilder sentenceSb = new StringBuilder();
				// List<HasWord> sentence = it.next();
				// for (HasWord token : sentence) {
				// if (sentenceSb.length() > 1) {
				// sentenceSb.append(" ");
				// }
				// sentenceSb.append(token);
				// }
				// urlTTS = new URL(
				// "http://localhost/TTSService/Service/service.svc/TTS?voiceIndex=1&rate=2&extension=mp3&text="
				// + sentenceSb.toString().replace(" ", "%20"));
				// System.out.println(urlTTS.toString());
				// HttpURLConnection urlConn = (HttpURLConnection) urlTTS
				// .openConnection();
				// urlConn.addRequestProperty("User-Agent",
				// "Mozilla/9.0 (compatible; MSIE 6.0; Windows NT 5.0)");
				// InputStream sound = urlConn.getInputStream();
				// FileOutputStream out = new FileOutputStream(file, true);
				// while ((len = sound.read(buf)) > 0) {
				// out.write(buf, 0, len);
				// }
				// out.close();
				// sound.close();
				// }

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//
			// additionTagToArticle
			// .addTagToArticle(article.getUrl().toString(),
			// OntonolyManagerSingleton.getInstance()
			// .getOntologyManager());
		}

		return articles;

	}

	public static void seperator(String s) {
		System.out
				.println("------------------------------------------------------------------------------------------");
		System.out.println(s);
	}

}
